package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.myapplication.util.ToastUtil;
import com.example.myapplication.util.UserRegistrationUtil; // 导入UserRegistrationUtil类

public class SignUpActivity extends AppCompatActivity {

    // 声明控件
    private Button mBtnSignUpSubmit;
    private EditText mEtUsername;
    private EditText mEtPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up);

        // 找到控件
        mBtnSignUpSubmit = findViewById(R.id.btn_signup_submit);
        mEtUsername = findViewById(R.id.et_username);
        mEtPassword = findViewById(R.id.et_password);

        // 设置注册按钮的点击事件
        mBtnSignUpSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 获取用户输入的用户名和密码
                String username = mEtUsername.getText().toString().trim();
                String password = mEtPassword.getText().toString().trim();

                // 调用UserRegistrationUtil注册新用户
                boolean isRegistered = UserRegistrationUtil.registerNewUser(SignUpActivity.this, username, password);

                if (isRegistered) {
                    // 注册成功
                    ToastUtil.showMsg(SignUpActivity.this, "注册成功，请返回登录");
                    // 跳转到主活动
                    Intent intent = new Intent(SignUpActivity.this, MainActivity.class);
                    startActivity(intent);
                } else {
                    // 注册失败
                    ToastUtil.showMsg(SignUpActivity.this, "注册失败，用户名可能已存在");
                }
            }
        });
    }
}